public class Solution {
    public int search(int[] nums, int target) {
        //常规解法
        // for(int i = 0;i < nums.length;i ++){
        //     if(nums[i] == target){
        //         return i;
        //     }
        // }
        // return -1;

        //二分查找
        int slow = 0;
        int high = nums.length - 1;


        while(slow <= high){
            //定义一个中间值进行二分查找
            int mid = (high  + slow ) / 2 ;
            if(nums[mid] > target){
                high = mid - 1;//如果相等就直接返回mid
            }else if(nums[mid] < target){
                slow = mid + 1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}